/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.practica.modelo.dao;



import com.practica.entidad.Declarante;
import com.practica.entidad.Parentesco;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Usuario23
 */
public class declaranteDAO {
        private EntityManagerFactory emd ;
        private EntityManager em;

    public declaranteDAO() {
        emd = Persistence.createEntityManagerFactory("TrabajoGrupo2PU");
        em=emd.createEntityManager();
        
       
    }
        
    public boolean insertarDeclarante(Declarante t)
    {
        boolean b= false;
        
        try{
            em.getTransaction().begin();
            em.persist(t);
            em.getTransaction().commit();
            b=true;
            
        } catch(Exception e){
            em.getTransaction().rollback();
        }
         return b;
         
    }
    
       public boolean  EliminarDeclarante(String codigo){
          boolean b= false;
       
        try{
            em.getTransaction().begin();
          
        Declarante t= em.find(Declarante.class, codigo);
        em.remove(t);
             em.getTransaction().commit(); 
          b=true;
             
        } catch(Exception e){
            em.getTransaction().rollback();
        }
         return b;
         
    }
    
    
    
    public Declarante buscarDeclarante(String codigo){
          
        Declarante t= em.find(Declarante.class, codigo);
        return t;
                
    }
    public List<Declarante> getListaDeclarantes(){
        Query q= em.createQuery("SELECT d FROM Declarante d ");//jpql
       
        return q.getResultList();
    }
    
  
    public void closeEntityManager(){
    em.close();
}
}
